package com.oet.web.freemarker;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.net.URLDecoder;
import java.io.UnsupportedEncodingException;

/**
 * @author sunwj
 * @version $Id: Function.java,v 1.1.1.1 2008/12/22 06:42:13 oy Exp $version $date $time $commiter Exp $
 * @since 2006-4-4
 */

/**
 * �����࣬     ȫ���Ǿ�̬��
 */
public class Function {
    private static String path = null;

    /**
     * �ж��ַ��Ƿ�Ϊ��
     * implemented by sunwj
     *
     * @since 2006-04-04
     */
    static {
        Function function = new Function();
        function.getUrlPath();
    }

    public static boolean isEmpty(String s) {
        return s == null || s.equals("");
    }

    public static int parseInt(String s, int defaultValue) {
        if (s == null || "".equals(s)) {
            return defaultValue;
        }

        int nValue = defaultValue;
        try {
            nValue = Integer.parseInt(s);
        } catch (Exception e) {
        }
        return nValue;
    }

    public static String normalizeString(String strValue) {
        return ((strValue == null) ? "" : strValue.trim());
    }

    /**
     * ����ǰ����ת��Ϊָ����ʽ���ַ�
     *
     * @param pattern ��ʽ
     * @return String
     */
    public static String getDateStringByPattern(String pattern) {
        Date date = new Date();
        return getDateStringByPattern(date, pattern);
    }

    /**
     * ��ָ������ת��Ϊָ����ʽ���ַ�
     *
     * @param date    ( java.core.Date )
     * @param pattern ���ڸ�ʽ���磺"yyyy-MM-dd" ��"yyyy-MM-dd HH:mm:ss"��"HH:mm:ss"
     * @return String
     */
    public static String getDateStringByPattern(Date date, String pattern) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat sf = new SimpleDateFormat(pattern);
        String str = sf.format(date);

        return str;
    }

    /**
     * ȡ��ϵͳ��ǰ��ʱ�䣬��Timestamp ��ʾ
     *
     * @ return ����Timestamp����
     */
    public static Timestamp getDateTime() {
        java.util.Date date = new java.util.Date();
        return (new java.sql.Timestamp(date.getTime()));
    }
    

    /**
     * the following const is to define date format.
     */


    public static java.util.Date parseDate(String strDate, String pattern) {
        if (isEmpty(strDate)) {
            return null;
        }
        java.text.SimpleDateFormat fmtDate = null;
        java.text.ParsePosition pos = new java.text.ParsePosition(0);
        fmtDate = new java.text.SimpleDateFormat(pattern);

        java.util.Date dtRet = null;
        try {
            return dtRet = fmtDate.parse(strDate, pos);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dtRet;

    }

    public static boolean exactEqual(String source, String target) {
        if (source == null || target == null) return false;
        if (source.length() != target.length()) return false;

        char[] sc = source.toCharArray();
        char[] tc = target.toCharArray();
        for (int i = 0; i < sc.length; i++) {
            if (sc[i] == tc[i]) {
                continue;
            } else {
                return false;
            }
        }
        return true;
    }

    /**
     * ��ȡϵͳ���·��
     *
     * @return
     */
    public final static String getPath() {
        if (Function.isEmpty(path)) {
            System.out.println("============");
            Function function = new Function();
            function.getUrlPath();
        }
        return path;
    }

    private final void getUrlPath() {
        try {
            String encoding = (String) System.getProperties().get("file.encoding");
            path = URLDecoder.decode(this.getClass().getResource("/").toString(), encoding);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {
	        if (System.getProperties().get("os.name").toString().toLowerCase().contains("windows")) {
	            path = path.substring(6, path.indexOf("WEB-INF"));
	        } else {
	            path = path.substring(5, path.indexOf("WEB-INF"));
	        }
        } catch (Exception e)
        {
        	e.printStackTrace();
        }
    }

    /**
     * ���������ҳ�ַ�ת��
     *
     * @param str
     * @return
     */
    public static final String changeEncoding(String str) {
        try {
            str = new String(str.getBytes("ISO8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            System.out.println("========������� ISO8859-1 --->>  UTF-8");
        }
        return str;
    }

    public static void main(String args[]) {
        /*for(Object kobj:System.getProperties().keySet()){
            System.out.println(kobj.toString()+"==="+System.getProperties().get(kobj.toString()));
        }*/
        for(int i=0;i<10;i++){
            System.out.println(Function.getPath());
        }

    }
}
